##### R CODE TO REPLICATE THE FIGURES AND TABLES INCLUDED IN 
##### Adrian Lucardi and Guilermo Rosas, "Is the Incumbent Curse the Incumbent’s Fault? Strategic Behavior and Negative Incumbency Effects in Young Democracies." Electoral Studies, 44, 2016, pp. 66-75.

## working directory -> change this to the directory where your have your data
setwd ("~/Dropbox/copartisanMayors/RD paper/Replication/")

## Loading the required R packages. Make sure to have them installed before proceeding. In order to install a package called X, write install.packages("X")
library (car)
library (foreign)
library (labeling)
library (plyr)
library (rdrobust)
library (texreg)
library (xtable)

## Important notice: we ran the RD using the rdrobust package. However, this package changed since then, and as a result the models no longer replicate exactly. To get an exact replication, you will need to download the old versions of the rdrobust() and rbwdselect() functions (provided):
source ("rdrobust0.80.R")
source ("rdbwselect.R")

## display options
options (show.signif.stars = FALSE, digits=3, scipen=5)
n.digit <- 2



###### DOWNLOADING AND CLEANING THE DATA

## downloading the data
base <- read.csv (file="Mexico municipal results by party 2000-2012.csv", sep=",", header=TRUE)  ## results by party
base2 <- read.csv (file="Mexico municipal results by governor 2000-2012.csv", sep=",", header=TRUE)  ## results by governor/runner-up's party
base.d <- read.csv (file="Mexico SMD results by party 2000-2012.csv", sep=",", header=TRUE)  ## SMD-level results

## Cleaning the data
# (a) We only include municipalities that were contained within a single district, i.e. we exclude those municipalities that were part of multiple districts
# (b) We do not include municipalities where two large parties (PAN-PRD, PAN-PRI or PRI-PRD) formed a common alliance in the previous election (we have already excluded these observations from the sample)
# (c) When looking at the effect of incumbency in federal elections, we will only include districts whose boundaries did not change as a consequence of a redistricting (change == 0) --which means we lose a lot of observations for 2006; when looking at municipal elections, however, this restriction does not apply

# (1) Party-based dataset
base <- base[base$subdistricts==1,] ## municipalities that are contained within a single SMD
base$short <- with (base, ifelse (base$time.since.muni < 3 & base$state!="JAL" & base$state!="MOR", 1, 0)) ## "short" dataset --> nonconcurrent elections only

## creating the variable that indicates whether a party's main rival controlled the governorship
base$gobInc.muni.oth <- with (base, ifelse (gobInc.muni==1, 0
                                            , ifelse ((pRup=="PAN" & incGober.muni=="PAN") | (pRup=="PRI" & incGober.muni=="PRI") | (pRup=="PRD" & incGober.muni=="PRD") | (pRup=="PAN.PRD" & incGober.muni=="PAN-PRD"), 1, 0)))
base$gobInc.muni <- with (base, ifelse (is.na (gobInc.muni), 0, gobInc.muni))
base$gobInc.muni.oth <- with (base, ifelse (is.na (gobInc.muni.oth), 0, gobInc.muni.oth))
base$gobInc.muni.3rd <- with (base, ifelse (gobInc.muni==0 & gobInc.muni.oth==0, 1, 0))
base$gobPAN.muni.oth <- with (base, ifelse (gobPAN.muni==1, 0
                                            , ifelse ((incGober.muni=="PRI" & (pFirst.rdd=="PRI" | pSecond.rdd=="PRI")) | (incGober.muni=="PRD" & (pFirst.rdd=="PRD" | pSecond.rdd=="PRD")), 1, 0))) ## if the PAN's main rival has a copartisan governor
base$gobPAN.muni.oth <- with (base, ifelse (is.na (gobPAN.muni.oth), 0, gobPAN.muni.oth))  ## there are a handful of NAs
base$gobPAN.muni.3rd <- with (base, ifelse (gobPAN.muni==0 & gobPAN.muni.oth==0, 1, 0)) ## governor is not a copartisan of either the PAN or its main rival
base$gobPRI.muni.oth <- with (base, ifelse (gobPRI.muni==1, 0
                                            , ifelse ((incGober.muni=="PAN" & (pFirst.rdd=="PAN" | pSecond.rdd=="PAN")) | (incGober.muni=="PRD" & (pFirst.rdd=="PRD" | pSecond.rdd=="PRD")), 1, 0)))
base$gobPRI.muni.oth <- with (base, ifelse (is.na (gobPRI.muni.oth), 0, gobPRI.muni.oth))
base$gobPRI.muni.3rd <- with (base, ifelse (gobPRI.muni==0 & gobPRI.muni.oth==0, 1, 0))
base$gobPRD.muni.oth <- with (base, ifelse (gobPRD.muni==1, 0
                                            , ifelse ((incGober.muni=="PAN" & (pFirst.rdd=="PAN" | pSecond.rdd=="PAN")) | (incGober.muni=="PRI" & (pFirst.rdd=="PRI" | pSecond.rdd=="PRI")), 1, 0)))
base$gobPRD.muni.oth <- with (base, ifelse (is.na (gobPRD.muni.oth), 0, gobPRD.muni.oth))
base$gobPRD.muni.3rd <- with (base, ifelse (gobPRD.muni==0 & gobPRD.muni.oth==0, 1, 0))

## recording the electoral outcome, at t+1, of the main rival of the PAN/PRI/PRD at t
base$firstPAN.riv.next <- with (base, ifelse (!(base$firstPAN==1 | base$secondPAN==1), NA, ifelse (mgPAN.muni.rdd>0, winSecond.muni.next, winFirst.muni.next))) ## indicates whether the main rival of the PAN in the municipal election at t won the municipal election at t+1
base$firstPRI.riv.next <- with (base, ifelse (!(base$firstPRI==1 | base$secondPRI==1), NA, ifelse (mgPRI.muni.rdd>0, winSecond.muni.next, winFirst.muni.next)))
base$firstPRD.riv.next <- with (base, ifelse (!(base$firstPRD==1 | base$secondPRD==1), NA, ifelse (mgPRD.muni.rdd>0, winSecond.muni.next, winFirst.muni.next)))

base$shPAN.riv.next <- with (base, ifelse (!(base$firstPAN==1 | base$secondPAN==1), NA, ifelse (mgPAN.muni.rdd>0, shSecond.muni.next, shFirst.muni.next)))
base$shPRI.riv.next <- with (base, ifelse (!(base$firstPRI==1 | base$secondPRI==1), NA, ifelse (mgPRI.muni.rdd>0, shSecond.muni.next, shFirst.muni.next)))
base$shPRD.riv.next <- with (base, ifelse (!(base$firstPRD==1 | base$secondPRD==1), NA, ifelse (mgPRD.muni.rdd>0, shSecond.muni.next, shFirst.muni.next)))

base$firstPAN.riv.fed <- with (base, ifelse (!(base$firstPAN==1 | base$secondPAN==1), NA, ifelse (mgPAN.muni.rdd>0, winSecond.fed, winFirst.fed)))
base$firstPRI.riv.fed <- with (base, ifelse (!(base$firstPRI==1 | base$secondPRI==1), NA, ifelse (mgPRI.muni.rdd>0, winSecond.fed, winFirst.fed)))
base$firstPRD.riv.fed <- with (base, ifelse (!(base$firstPRD==1 | base$secondPRD==1), NA, ifelse (mgPRD.muni.rdd>0, winSecond.fed, winFirst.fed)))

base$shPAN.riv.fed <- with (base, ifelse (!(base$firstPAN==1 | base$secondPAN==1), NA, ifelse (mgPAN.muni.rdd>0, shSecond.fed, shFirst.fed)))
base$shPRI.riv.fed <- with (base, ifelse (!(base$firstPRI==1 | base$secondPRI==1), NA, ifelse (mgPRI.muni.rdd>0, shSecond.fed, shFirst.fed)))
base$shPRD.riv.fed <- with (base, ifelse (!(base$firstPRD==1 | base$secondPRD==1), NA, ifelse (mgPRD.muni.rdd>0, shSecond.fed, shFirst.fed)))

# re-factorizing and eliminating variables that take constant values:
base$muniYear <- with (base, factor (paste (muni, year.muni, sep="_")))
base$muni <- factor (base$muni)
base$state <- factor (base$state)
base$stateYear.next <- factor (base$stateYear.next)
base$incGober.muni <- factor (base$incGober.muni)
base$runnerUp.muni <- factor (base$runnerUp.muni)
base$incGober.fed <- factor (base$incGober.fed)
base$runnerUp.fed <- factor (base$runnerUp.fed)
base$district <- factor (base$district)
base$muniDistrictYear <- factor (base$muniDistrictYear)
base$pFirst.rdd <- factor (base$pFirst.rdd)
base$pSecond.rdd <- factor (base$pSecond.rdd)
base$pThird.rdd <- factor (base$pThird.rdd)
base$pFirst.muni.next <- factor (base$pFirst.muni.next)
base$pSecond.muni.next <- factor (base$pSecond.muni.next)
base$pThird.muni.next <- factor (base$pThird.muni.next)
base$pFirst.fed <- factor (base$pFirst.fed)
base$pSecond.fed <- factor (base$pSecond.fed)
base$pThird.fed <- factor (base$pThird.fed)
base$pInc <- factor (base$pInc)
base$pRup <- factor (base$pRup)
base$runoff <- NULL
base$reverse.runoff <- NULL
base$year.muni.lag <- NULL
base$allyPAN <- NULL
base$allyPRI <- NULL
base$allyPRD <- NULL
base$subdistricts <- NULL
base$muniProp <- NULL

base$year.fed2 <- factor (base$year.fed)
base$year.muni2 <- factor (base$year.muni)
base$year.muni.next2 <- factor (base$year.muni.next)
summary (base); dim (base)


## (2) SMD-level dataset

## creating the variable that indicates whether a party's main rival controlled the governorship
base.d$gobInc.oth <- with (base.d, ifelse (gobInc==1, 0
                                           , ifelse ((pRup=="PAN" & incGober=="PAN") | (pRup=="PRI" & incGober=="PRI") | (pRup=="PRD" & incGober=="PRD") | (pRup=="PVEM" & incGober=="PVEM"), 1, 0)))
base.d$gobInc <- with (base.d, ifelse (is.na (gobInc), 0, gobInc))
base.d$gobInc.oth <- with (base.d, ifelse (is.na (gobInc.oth), 0, gobInc.oth))
base.d$gobInc.3rd <- with (base.d, ifelse (gobInc==0 & gobInc.oth==0, 1, 0))
base.d$gobPAN.oth <- with (base.d, ifelse (gobPAN==1, 0, ifelse ((incGober=="PRI" & (pFirst=="PRI" | pSecond=="PRI")) | (incGober=="PRD" & (pFirst=="PRD" | pSecond=="PRD")) | (incGober=="PVEM" & (pFirst=="PVEM" | pSecond=="PVEM")), 1, 0)))
base.d$gobPAN.oth <- with (base.d, ifelse (is.na (gobPAN.oth), 0, gobPAN.oth))
base.d$gobPAN.3rd <- with (base.d, ifelse (gobPAN==0 & gobPAN.oth==0, 1, 0))
base.d$gobPRI.oth <- with (base.d, ifelse (gobPRI==1, 0, ifelse ((incGober=="PAN" & (pFirst=="PAN" | pSecond=="PAN")) | (incGober=="PRD" & (pFirst=="PRD" | pSecond=="PRD")) | (incGober=="PVEM" & (pFirst=="PVEM" | pSecond=="PVEM")), 1, 0)))
base.d$gobPRI.oth <- with (base.d, ifelse (is.na (gobPRI.oth), 0, gobPRI.oth))
base.d$gobPRI.3rd <- with (base.d, ifelse (gobPRI==0 & gobPRI.oth==0, 1, 0))
base.d$gobPRD.oth <- with (base.d, ifelse (gobPRD==1, 0, ifelse ((incGober=="PAN" & (pFirst=="PAN" | pSecond=="PAN")) | (incGober=="PRI" & (pFirst=="PRI" | pSecond=="PRI")) | (incGober=="PVEM" & (pFirst=="PVEM" | pSecond=="PVEM")), 1, 0)))
base.d$gobPRD.oth <- with (base.d, ifelse (is.na (gobPRD.oth), 0, gobPRD.oth))
base.d$gobPRD.3rd <- with (base.d, ifelse (gobPRD==0 & gobPRD.oth==0, 1, 0))


## identifying states with concurrent elections
base.d$conc <- with (base.d, ifelse (state=="CAM" | state=="CMA" | state=="DF" | state=="EDOMEX" | state=="GTO" | state=="JAL" | state=="MOR" | state=="NL" | state=="QUER" | state=="SLP" | state=="SON" | state=="TAB" | (state=="VER" & year <= 2000), 1, 0)) ## 12 states plus Veracruz before 2003 had concurrent elections
base.d$districtYear <- factor (base.d$districtYear)
base.d$state <- factor (base.d$state)
base.d$district <- factor (base.d$district)
base.d$pFirst <- factor (base.d$pFirst)
base.d$pSecond <- factor (base.d$pSecond)
base.d$pThird <- factor (base.d$pThird)
base.d$pFirst.next <- factor (base.d$pFirst.next)
base.d$pSecond.next <- factor (base.d$pSecond.next)
base.d$pThird.next <- factor (base.d$pThird.next)
base.d$pInc <- factor (base.d$pInc)
base.d$pRup <- factor (base.d$pRup)
base.d$incGober <- factor (base.d$incGober)
base.d$runnerUp <- factor (base.d$runnerUp)
base.d$year2 <- factor (base.d$year)
base.d$year.next2 <- factor (base.d$year + 3)
summary (base.d); dim (base.d)

# non-concurrent states only
base.d2 <- base.d[base.d$conc==0,]
base.d2$districtYear <- factor (base.d2$districtYear)
base.d2$state <- factor (base.d2$state)
base.d2$district <- factor (base.d2$district)
base.d2$pFirst <- factor (base.d2$pFirst)
base.d2$pSecond <- factor (base.d2$pSecond)
base.d2$pThird <- factor (base.d2$pThird)
base.d2$pFirst.next <- factor (base.d2$pFirst.next)
base.d2$pSecond.next <- factor (base.d2$pSecond.next)
base.d2$pThird.next <- factor (base.d2$pThird.next)
base.d2$pInc <- factor (base.d2$pInc)
base.d2$pRup <- factor (base.d2$pRup)
base.d2$incGober <- factor (base.d2$incGober)
base.d2$runnerUp <- factor (base.d2$runnerUp)
base.d2$year2 <- factor (base.d2$year)
base.d2$year.next2 <- factor (base.d2$year + 3)
base.d2$conc <- NULL
summary (base.d2); dim (base.d2)




########## REPLICATING THE RESULTS

### getting the reelection rates mentioned in p. 68
with (base[base$firstInc.muni==1,], mean (firstInc.next, na.rm=T))
with (base.d[base.d$firstInc==1,], mean (firstInc.next, na.rm=T))


### replicating the observational results mentioned in p. 68

# changing the contrasts in order to estimate predicted probabilities
contrasts (base$year.muni.next2) <- "contr.sum"
contrasts (base$state) <- "contr.sum"
contrasts (base.d2$year.next2) <- "contr.sum"
contrasts (base.d2$state) <- "contr.sum"

# (a) Municipal elections
summary (lm.pan01 <- lm (firstPAN.next ~ firstPAN + shPAN + year.muni.next2 + state, data=base))
summary (lm.pri01 <- lm (firstPRI.next ~ firstPRI + shPRI + year.muni.next2 + state, data=base))
summary (lm.prd01 <- lm (firstPRD.next ~ firstPRD + shPRD + year.muni.next2 + state, data=base))

# predicted probabilities with and without incumbency (assuming vote share (t-1) = 0.40)
(pan0 <- coef (lm.pan01)[1] + coef (lm.pan01)[2]*0 + coef (lm.pan01)[3]*0.4)
(pan1 <- coef (lm.pan01)[1] + coef (lm.pan01)[2]*1 + coef (lm.pan01)[3]*0.4)

(pri0 <- coef (lm.pri01)[1] + coef (lm.pri01)[2]*0 + coef (lm.pri01)[3]*0.4)
(pri1 <- coef (lm.pri01)[1] + coef (lm.pri01)[2]*1 + coef (lm.pri01)[3]*0.4)

(prd0 <- coef (lm.prd01)[1] + coef (lm.prd01)[2]*0 + coef (lm.prd01)[3]*0.4)
(prd1 <- coef (lm.prd01)[1] + coef (lm.prd01)[2]*1 + coef (lm.prd01)[3]*0.4)


# (b) Federal elections
summary (lm.fed.pan01 <- lm (firstPAN.next ~ firstPAN + shPAN + year.next2 + state, data=base.d))
summary (lm.fed.pri01 <- lm (firstPRI.next ~ firstPRI + shPRI + year.next2 + state, data=base.d))
summary (lm.fed.prd01 <- lm (firstPRD.next ~ firstPRD + shPRD + year.next2 + state, data=base.d))

# predicted probabilities with and without incumbency
(pan0 <- coef (lm.fed.pan01)[1] + coef (lm.fed.pan01)[2]*0 + coef (lm.fed.pan01)[3]*0.4)
(pan1 <- coef (lm.fed.pan01)[1] + coef (lm.fed.pan01)[2]*1 + coef (lm.fed.pan01)[3]*0.4)

(pri0 <- coef (lm.fed.pri01)[1] + coef (lm.fed.pri01)[2]*0 + coef (lm.fed.pri01)[3]*0.4)
(pri1 <- coef (lm.fed.pri01)[1] + coef (lm.fed.pri01)[2]*1 + coef (lm.fed.pri01)[3]*0.4)

# (c) PRD
(prd0 <- coef (lm.fed.prd01)[1] + coef (lm.fed.prd01)[2]*0 + coef (lm.fed.prd01)[3]*0.4)
(prd1 <- coef (lm.fed.prd01)[1] + coef (lm.fed.prd01)[2]*1 + coef (lm.fed.prd01)[3]*0.4)

# getting the old contrasts back
contrasts (base$year.muni.next2) <- "contr.treatment"
contrasts (base$state) <- "contr.treatment"
contrasts (base.d2$year.next2) <- "contr.treatment"
contrasts (base.d2$state) <- "contr.treatment"



### Replicating the models in Table 1

## panel (a)
(rddWin2.inc01a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan01a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri01a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd01a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))

## panel (b)
(rddWin.smd.inc01a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan01a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri01a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd01a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))



### Replicating the models in Table 2

## Identifying "close" elections --> we follow the banwidth choice of rdrobust()
base$clInca <- with (base, ifelse (!(base$firstInc.muni==1 | base$secondInc.muni==1), NA, ifelse (abs (mgInc.muni.rdd) <= rddWin2.pan01a$bws[1], 1, 0)))
base$clPANa <- with (base, ifelse (!(base$firstPAN==1 | base$secondPAN==1), NA, ifelse (abs (mgPAN.muni.rdd) <= rddWin2.pan01a$bws[1], 1, 0)))
base$clPRIa <- with (base, ifelse (!(base$firstPRI==1 | base$secondPRI==1), NA, ifelse (abs (mgPRI.muni.rdd) <= rddWin2.pan01a$bws[1], 1, 0)))
base$clPRDa <- with (base, ifelse (!(base$firstPRD==1 | base$secondPRD==1), NA, ifelse (abs (mgPRD.muni.rdd) <= rddWin2.pan01a$bws[1], 1, 0)))

## Calculating the CHANGES in vote shares
base$shThird.muni.ch.Inc01 <- with (base, ifelse (is.na (clInca), NA, shThird.muni.next - shThird.muni))  ## we have different variables, because we define "close" election on the basis of different samples
base$shThird.muni.ch.PAN01 <- with (base, ifelse (is.na (clPANa), NA, shThird.muni.next - shThird.muni))
base$shThird.muni.ch.PRI01 <- with (base, ifelse (is.na (clPRIa), NA, shThird.muni.next - shThird.muni))
base$shThird.muni.ch.PRD01 <- with (base, ifelse (is.na (clPRDa), NA, shThird.muni.next - shThird.muni))

## Running the models
summary (lmClose.inc01a <- lm (shThird.muni.ch.Inc01 ~ clInca, data=base))
summary (lmClose.inc01b <- lm (shThird.muni.ch.Inc01 ~ abs (mgInc.muni.rdd), data=base))
summary (lmClose.inc01c <- lm (shThird.muni.ch.Inc01 ~ abs (mgInc.muni.rdd), data=base[base$mgInc.muni.rdd < 0,]))
summary (lmClose.inc01d <- lm (shThird.muni.ch.Inc01 ~ abs (mgInc.muni.rdd), data=base[base$mgInc.muni.rdd > 0,]))
summary (lmClose.inc01e <- lm (shThird.muni.ch.Inc01 ~ abs (mgInc.muni.rdd), data=base[base$clInca==1,]))

summary (lmClose.pan01a <- lm (shThird.muni.ch.PAN01 ~ clPANa, data=base))
summary (lmClose.pan01b <- lm (shThird.muni.ch.PAN01 ~ abs (mgPAN.muni.rdd), data=base))
summary (lmClose.pan01c <- lm (shThird.muni.ch.PAN01 ~ abs (mgPAN.muni.rdd), data=base[base$mgPAN.muni.rdd < 0,]))
summary (lmClose.pan01d <- lm (shThird.muni.ch.PAN01 ~ abs (mgPAN.muni.rdd), data=base[base$mgPAN.muni.rdd > 0,]))
summary (lmClose.pan01e <- lm (shThird.muni.ch.PAN01 ~ abs (mgPAN.muni.rdd), data=base[base$clPANa==1,]))

summary (lmClose.pri01a <- lm (shThird.muni.ch.PRI01 ~ clPRIa, data=base))
summary (lmClose.pri01b <- lm (shThird.muni.ch.PRI01 ~ abs (mgPRI.muni.rdd), data=base))
summary (lmClose.pri01c <- lm (shThird.muni.ch.PRI01 ~ abs (mgPRI.muni.rdd), data=base[base$mgPRI.muni.rdd < 0,]))
summary (lmClose.pri01d <- lm (shThird.muni.ch.PRI01 ~ abs (mgPRI.muni.rdd), data=base[base$mgPRI.muni.rdd > 0,]))
summary (lmClose.pri01e <- lm (shThird.muni.ch.PRI01 ~ abs (mgPRI.muni.rdd), data=base[base$clPRIa==1,]))

summary (lmClose.prd01a <- lm (shThird.muni.ch.PRD01 ~ clPRDa, data=base))
summary (lmClose.prd01b <- lm (shThird.muni.ch.PRD01 ~ abs (mgPRD.muni.rdd), data=base))
summary (lmClose.prd01c <- lm (shThird.muni.ch.PRD01 ~ abs (mgPRD.muni.rdd), data=base[base$mgPRD.muni.rdd < 0,]))
summary (lmClose.prd01d <- lm (shThird.muni.ch.PRD01 ~ abs (mgPRD.muni.rdd), data=base[base$mgPRD.muni.rdd > 0,]))
summary (lmClose.prd01e <- lm (shThird.muni.ch.PRD01 ~ abs (mgPRD.muni.rdd), data=base[base$clPRDa==1,]))



### Replicating the models in Table 3

# vote share
(rddSh2.inc01a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=shInc.muni.next, x=mgInc.muni.rdd, all=T)))
(rddSh2.pan01a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=shPAN.muni.next, x=mgPAN.muni.rdd, all=T)))
(rddSh2.pri01a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=shPRI.muni.next, x=mgPRI.muni.rdd, all=T)))
(rddSh2.prd01a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=shPRD.muni.next, x=mgPRD.muni.rdd, all=T)))

# winner (oth)
(rddWin2.inc02a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.riv.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan02a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.riv.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri02a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.riv.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd02a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.riv.next, x=mgPRD.muni.rdd, all=T)))

# vote share (oth)
(rddSh2.inc02a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=shInc.riv.next, x=mgInc.muni.rdd, all=T)))
(rddSh2.pan02a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=shPAN.riv.next, x=mgPAN.muni.rdd, all=T)))
(rddSh2.pri02a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=shPRI.riv.next, x=mgPRI.muni.rdd, all=T)))
(rddSh2.prd02a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=shPRD.riv.next, x=mgPRD.muni.rdd, all=T)))


### Bootstrapping the difference between a party's lose (in votes) and its rival's gain (p. 72)

## NOTE: the bootstrap takes several minutes to run
set.seed (85962)  ## for reproducibility
m <- 250  ## number of bootstraps

coefs.full <- as.data.frame (matrix (NA, ncol=4, nrow=m))
colnames (coefs.full) <- c ("allLinear", "panLinear", "priLinear", "prdLinear")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    # all incumbents
    coefs.full[i,1] <- with (base.tmp[(base.tmp$firstInc.muni==1 | base.tmp$secondInc.muni==1),], rdrobust (y=shInc.riv.next, x=mgInc.muni.rdd, all=T)$coef[2] + rdrobust (y=shInc.muni.next, x=mgInc.muni.rdd, all=T)$coef[2])

    # PAN
    coefs.full[i,2] <- with (base.tmp[(base.tmp$firstPAN==1 | base.tmp$secondPAN==1),], rdrobust (y=shPAN.riv.next, x=mgPAN.muni.rdd, all=T)$coef[2] + rdrobust (y=shPAN.muni.next, x=mgPAN.muni.rdd, all=T)$coef[2])

    # PRI
    coefs.full[i,3] <- with (base.tmp[(base.tmp$firstPRI==1 | base.tmp$secondPRI==1),], rdrobust (y=shPRI.riv.next, x=mgPRI.muni.rdd, all=T)$coef[2] + rdrobust (y=shPRI.muni.next, x=mgPRI.muni.rdd, all=T)$coef[2])

    # PRD
    coefs.full[i,4] <- with (base.tmp[(base.tmp$firstPRD==1 | base.tmp$secondPRD==1),], rdrobust (y=shPRD.riv.next, x=mgPRD.muni.rdd, all=T)$coef[2] + rdrobust (y=shPRD.muni.next, x=mgPRD.muni.rdd, all=T)$coef[2])
  })
summary (coefs.full)

## Examining the results:
colMeans (coefs.full)*100 ## essentially 0 for the PAN and PRD; positive but small (1 and 1.9pp) for the incumbent party and the PRI, respectively
quantile (coefs.full$allLinear, probs=c (0.025, 0.05, 0.1, 0.15, 0.2))*100
quantile (coefs.full$panLinear, probs=c (0.025, 0.05, 0.1, 0.15, 0.2))*100
quantile (coefs.full$priLinear, probs=c (0.025, 0.05, 0.1, 0.15, 0.2))*100
quantile (coefs.full$prdLinear, probs=c (0.025, 0.05, 0.1, 0.15, 0.2))*100



### Replicating the models in Table 4

# variable indicating elections in which the (a) PRI first and PAN second, OR (b) PRI second and PAN first
base$PRIvsPAN <- with (base, ifelse ((pFirst.rdd=="PRI" & pSecond.rdd=="PAN") | (pFirst.rdd=="PAN" & pSecond.rdd=="PRI"), 1, 0))
base$PRIvsPRD <- with (base, ifelse ((pFirst.rdd=="PRI" & pSecond.rdd=="PRD") | (pFirst.rdd=="PRD" & pSecond.rdd=="PRI"), 1, 0))

# PAN
(rddWin2.pri05a <- with (base[base$PRIvsPAN==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.pri05b <- with (base[base$PRIvsPAN==1,], rdrobust (y=firstPAN.next, x=mgPRI.muni.rdd, all=T)))

# PRD
(rddWin2.pri06a <- with (base[base$PRIvsPRD==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.pri06b <- with (base[base$PRIvsPRD==1,], rdrobust (y=firstPRD.next, x=mgPRI.muni.rdd, all=T)))



### Bootstrapping the difference between parties' probabilities of winning (p. 73)

## NOTE: the bootstrap takes several minutes to run
set.seed (97592)  ## for reproducibility
m <- 250

coefs.2party <- as.data.frame (matrix (NA, ncol=4, nrow=m))
colnames (coefs.2party) <- c ("vsPAN_PRIwin", "vsPAN_PANwin", "vsPRD_PRIwin", "vsPRD_PRDwin")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    # PRI vs. PAN (winner)
    coefs.2party[i,1] <- with (base.tmp[base.tmp$PRIvsPAN==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T))$coef[2]
    coefs.2party[i,2] <- with (base.tmp[base.tmp$PRIvsPAN==1,], rdrobust (y=firstPAN.next, x=mgPRI.muni.rdd, all=T))$coef[2]

    # PRI vs. PRD (winner)
    coefs.2party[i,3] <- with (base.tmp[base.tmp$PRIvsPRD==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T))$coef[2]
    coefs.2party[i,4] <- with (base.tmp[base.tmp$PRIvsPRD==1,], rdrobust (y=firstPRD.next, x=mgPRI.muni.rdd, all=T))$coef[2]
  })
summary (coefs.2party)

coefs.2party$PANbetterWin <- with (coefs.2party, ifelse (vsPAN_PRIwin < (-1)*vsPAN_PANwin, 1, 0))
coefs.2party$PANbetterSh <- with (coefs.2party, ifelse (vsPAN_PRIsh < (-1)*vsPAN_PANsh, 1, 0))
coefs.2party$PRDbetterWin <- with (coefs.2party, ifelse (vsPRD_PRIwin < (-1)*vsPRD_PRDwin, 1, 0))
coefs.2party$PRDbetterSh <- with (coefs.2party, ifelse (vsPRD_PRIsh < (-1)*vsPRD_PRDsh, 1, 0))



### Replicating the models in Table 5

(rddWin2.inc03a <- with (base[base$gobInc.muni==1 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.inc04a <- with (base[base$gobInc.muni.oth==1 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan03a <- with (base[base$gobPAN.muni==1 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pan04a <- with (base[base$gobPAN.muni.oth==1 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri03a <- with (base[base$gobPRI.muni==1 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.pri04a <- with (base[base$gobPRI.muni.oth==1 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd03a <- with (base[base$gobPRD.muni==1 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))
(rddWin2.prd04a <- with (base[base$gobPRD.muni.oth==1 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))


### Bootstrapping the difference between having a copartisna governor and not having one (pp. 73-4)

## NOTE: the bootstrap takes several minutes to run
set.seed (199719)  ## for reproducibility
m <- 250  ## number of bootstraps

# Incumbent party
coefs.inc <- as.data.frame (matrix (NA, ncol=2, nrow=m))
colnames (coefs.inc) <- c ("cct.inc", "cct.opp")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    ## saving the coefficients
    coefs.inc[i,1] <- with (base.tmp[(base.tmp$firstInc.muni==1 | base.tmp$secondInc.muni==1) & base.tmp$gobInc.muni==1,], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T))$coef[2]
    coefs.inc[i,2] <- with (base.tmp[(base.tmp$firstInc.muni==1 | base.tmp$secondInc.muni==1) & base.tmp$gobInc.muni.oth==1,], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T))$coef[2]
  })

# PAN
coefs.pan <- as.data.frame (matrix (NA, ncol=2, nrow=m))
colnames (coefs.pan) <- c ("cct.inc", "cct.opp")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    ## saving the coefficients
    coefs.pan[i,1] <- with (base.tmp[(base.tmp$firstPAN==1 | base.tmp$secondPAN==1) & base.tmp$gobPAN.muni==1,], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T))$coef[2]
    coefs.pan[i,2] <- with (base.tmp[(base.tmp$firstPAN==1 | base.tmp$secondPAN==1) & base.tmp$gobPAN.muni.oth==1,], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T))$coef[2]
  })

# PRI
coefs.pri <- as.data.frame (matrix (NA, ncol=2, nrow=m))
colnames (coefs.pri) <- c ("cct.inc", "cct.opp")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    ## saving the coefficients
    coefs.pri[i,1] <- with (base.tmp[(base.tmp$firstPRI==1 | base.tmp$secondPRI==1) & base.tmp$gobPRI.muni==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T))$coef[2]
    coefs.pri[i,2] <- with (base.tmp[(base.tmp$firstPRI==1 | base.tmp$secondPRI==1) & base.tmp$gobPRI.muni.oth==1,], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T))$coef[2]
  })

# PRD
coefs.prd <- as.data.frame (matrix (NA, ncol=2, nrow=m))
colnames (coefs.prd) <- c ("cct.inc", "cct.opp")

system.time (
  for (i in 1:m){

    ## selecting the sample
    samp <- sample (x=1:nrow (base), size=nrow (base), replace=TRUE)
    base.tmp <- base[samp,]

    ## saving the coefficients
    coefs.prd[i,1] <- with (base.tmp[(base.tmp$firstPRD==1 | base.tmp$secondPRD==1) & base.tmp$gobPRD.muni==1,], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T))$coef[2]
    coefs.prd[i,2] <- with (base.tmp[(base.tmp$firstPRD==1 | base.tmp$secondPRD==1) & base.tmp$gobPRD.muni.oth==1,], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T))$coef[2]
  })

## Getting the p-values
coefs.inc$cct.dif1 <- with (coefs.inc, ifelse (cct.inc > cct.opp, 0, 1))
coefs.pan$cct.dif1 <- with (coefs.pan, ifelse (cct.inc > cct.opp, 0, 1))
coefs.pri$cct.dif1 <- with (coefs.pri, ifelse (cct.inc > cct.opp, 0, 1))
coefs.prd$cct.dif1 <- with (coefs.prd, ifelse (cct.inc > cct.opp, 0, 1))
colMeans (coefs.inc[,4])
colMeans (coefs.pan[,4])
colMeans (coefs.pri[,4])
colMeans (coefs.prd[,4])





##############################  ONLINE APPENDICES  ##############################

### Replicating the models in Table A2

### Full sample

## panel (a)
(rddWin2.inc01a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan01a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri01a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd01a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))

## panel (b)
(rddWin.smd.inc01a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan01a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri01a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd01a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))


### Non-concurrent states only

## panel (a)
(rddWin2.inc01b <- with (base[base$short==1 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan01b <- with (base[base$short==1 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri01b <- with (base[base$short==1 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd01b <- with (base[base$short==1 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))

## panel (b)
(rddWin.smd.inc01b <- with (base.d2[(base.d2$firstInc==1 | base.d2$secondInc==1),], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan01b <- with (base.d2[base.d2$firstPAN==1 | base.d2$secondPAN==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri01b <- with (base.d2[base.d2$firstPRI==1 | base.d2$secondPRI==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd01b <- with (base.d2[base.d2$firstPRD==1 | base.d2$secondPRD==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))

### Concurrent states only

## panel (a)
(rddWin2.inc01c <- with (base[base$short==0 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.next, x=mgInc.muni.rdd, all=T)))
(rddWin2.pan01c <- with (base[base$short==0 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN.muni.rdd, all=T)))
(rddWin2.pri01c <- with (base[base$short==0 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI.muni.rdd, all=T)))
(rddWin2.prd01c <- with (base[base$short==0 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD.muni.rdd, all=T)))

## panel (b)
(rddWin.smd.inc01c <- with (base.d[base.d$conc==1 & (base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan01c <- with (base.d[base.d$conc==1 & (base.d$firstPAN==1 | base.d$secondPAN==1),], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri01c <- with (base.d[base.d$conc==1 & (base.d$firstPRI==1 | base.d$secondPRI==1),], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd01c <- with (base.d[base.d$conc==1 & (base.d$firstPRD==1 | base.d$secondPRD==1),], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))



### Replicating the models in Table A3

## panel (a)
(rddWin.inc01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.fed, x=mgInc.muni.rdd, all=T)))
(rddWin.pan01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.fed, x=mgPAN.muni.rdd, all=T)))
(rddWin.pri01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.fed, x=mgPRI.muni.rdd, all=T)))
(rddWin.prd01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.fed, x=mgPRD.muni.rdd, all=T)))

(rddSh.inc01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=shInc.fed, x=mgInc.muni.rdd, all=T)))
(rddSh.pan01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=shPAN.fed, x=mgPAN.muni.rdd, all=T)))
(rddSh.pri01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=shPRI.fed, x=mgPRI.muni.rdd, all=T)))
(rddSh.prd01a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=shPRD.fed, x=mgPRD.muni.rdd, all=T)))

(rddWin.inc02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.riv.fed, x=mgInc.muni.rdd, all=T)))
(rddWin.pan02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.riv.fed, x=mgPAN.muni.rdd, all=T)))
(rddWin.pri02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.riv.fed, x=mgPRI.muni.rdd, all=T)))
(rddWin.prd02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.riv.fed, x=mgPRD.muni.rdd, all=T)))

(rddSh.inc02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=shInc.riv.fed, x=mgInc.muni.rdd, all=T)))
(rddSh.pan02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=shPAN.riv.fed, x=mgPAN.muni.rdd, all=T)))
(rddSh.pri02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=shPRI.riv.fed, x=mgPRI.muni.rdd, all=T)))
(rddSh.prd02a <- with (base[(!is.na (base$change) & base$change==0) & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=shPRD.riv.fed, x=mgPRD.muni.rdd, all=T)))

## panel (b)
(rddWin.smd.inc01a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan01a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri01a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd01a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))

(rddSh.smd.inc01a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=shInc.next, x=mgInc, all=T)))
(rddSh.smd.pan01a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=shPAN.next, x=mgPAN, all=T)))
(rddSh.smd.pri01a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=shPRI.next, x=mgPRI, all=T)))
(rddSh.smd.prd01a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=shPRD.next, x=mgPRD, all=T)))

(rddWin.smd.inc02a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=firstInc.riv.next, x=mgInc, all=T)))
(rddWin.smd.pan02a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=firstPAN.riv.next, x=mgPAN, all=T)))
(rddWin.smd.pri02a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=firstPRI.riv.next, x=mgPRI, all=T)))
(rddWin.smd.prd02a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=firstPRD.riv.next, x=mgPRD, all=T)))

(rddSh.smd.inc02a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=shInc.riv.next, x=mgInc, all=T)))
(rddSh.smd.pan02a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=shPAN.riv.next, x=mgPAN, all=T)))
(rddSh.smd.pri02a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=shPRI.riv.next, x=mgPRI, all=T)))
(rddSh.smd.prd02a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=shPRD.riv.next, x=mgPRD, all=T)))



### Replicating the models in Table A4

# Panel (a)
(rddWin.inc03a <- with (base[(!is.na (base$change) & base$change==0) & base$gobInc.muni==1 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.fed, x=mgInc.muni.rdd, all=T)))
(rddWin.inc04a <- with (base[(!is.na (base$change) & base$change==0) & base$gobInc.muni.oth==1 & (base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=firstInc.fed, x=mgInc.muni.rdd, all=T)))
(rddWin.pan03a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPAN.muni==1 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.fed, x=mgPAN.muni.rdd, all=T)))
(rddWin.pan04a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPAN.muni.oth==1 & (base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.fed, x=mgPAN.muni.rdd, all=T)))
(rddWin.pri03a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPRI.muni==1 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.fed, x=mgPRI.muni.rdd, all=T)))
(rddWin.pri04a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPRI.muni.oth==1 & (base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.fed, x=mgPRI.muni.rdd, all=T)))
(rddWin.prd03a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPRD.muni==1 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.fed, x=mgPRD.muni.rdd, all=T)))
(rddWin.prd04a <- with (base[(!is.na (base$change) & base$change==0) & base$gobPRD.muni.oth==1 & (base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.fed, x=mgPRD.muni.rdd, all=T)))

# Panel (b)
(rddWin.smd.inc03a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1) & base.d$gobInc==1,], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.inc04a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1) & base.d$gobInc.oth==1,], rdrobust (y=firstInc.next, x=mgInc, all=T)))
(rddWin.smd.pan03a <- with (base.d[(base.d$firstPAN==1 | base.d$secondPAN==1) & base.d$gobPAN==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pan04a <- with (base.d[(base.d$firstPAN==1 | base.d$secondPAN==1) & base.d$gobPAN.oth==1,], rdrobust (y=firstPAN.next, x=mgPAN, all=T)))
(rddWin.smd.pri03a <- with (base.d[(base.d$firstPRI==1 | base.d$secondPRI==1) & base.d$gobPRI==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.pri04a <- with (base.d[(base.d$firstPRI==1 | base.d$secondPRI==1) & base.d$gobPRI.oth==1,], rdrobust (y=firstPRI.next, x=mgPRI, all=T)))
(rddWin.smd.prd03a <- with (base.d[(base.d$firstPRD==1 | base.d$secondPRD==1) & base.d$gobPRD==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))
(rddWin.smd.prd04a <- with (base.d[(base.d$firstPRD==1 | base.d$secondPRD==1) & base.d$gobPRD.oth==1,], rdrobust (y=firstPRD.next, x=mgPRD, all=T)))



### Replicating the models in Table A5

## Panel (a)

# Winner at t-1 as the outcome
(rddBal.pan01a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=firstPAN.lag, x=mgPAN.muni.rdd, all=T)))
(rddBal.pri01a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=firstPRI.lag, x=mgPRI.muni.rdd, all=T)))
(rddBal.prd01a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=firstPRD.lag, x=mgPRD.muni.rdd, all=T)))

# Vote share at t-1 as the outcome
(rddBal.inc02a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=shInc.lag, x=mgInc.muni.rdd, all=T)))
(rddBal.pan02a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=shPAN.lag, x=mgPAN.muni.rdd, all=T)))
(rddBal.pri02a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=shPRI.lag, x=mgPRI.muni.rdd, all=T)))
(rddBal.prd02a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=shPRD.lag, x=mgPRD.muni.rdd, all=T)))

# Population (log) as the outcome
(rddBal.inc03a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=log (population), x=mgInc.muni.rdd, all=T)))
(rddBal.pan03a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=log (population), x=mgPAN.muni.rdd, all=T)))
(rddBal.pri03a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=log (population), x=mgPRI.muni.rdd, all=T)))
(rddBal.prd03a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=log (population), x=mgPRD.muni.rdd, all=T)))

# Poverty as the outcome
(rddBal.inc04a <- with (base[(base$firstInc.muni==1 | base$secondInc.muni==1),], rdrobust (y=poverty, x=mgInc.muni.rdd, all=T)))
(rddBal.pan04a <- with (base[(base$firstPAN==1 | base$secondPAN==1),], rdrobust (y=poverty, x=mgPAN.muni.rdd, all=T)))
(rddBal.pri04a <- with (base[(base$firstPRI==1 | base$secondPRI==1),], rdrobust (y=poverty, x=mgPRI.muni.rdd, all=T)))
(rddBal.prd04a <- with (base[(base$firstPRD==1 | base$secondPRD==1),], rdrobust (y=poverty, x=mgPRD.muni.rdd, all=T)))


## Panel (b)

# Winner at t-1 as the outcome
(rddBal.smd.pan01a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=firstPAN.lag, x=mgPAN, all=T)))
(rddBal.smd.pri01a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=firstPRI.lag, x=mgPRI, all=T)))
(rddBal.smd.prd01a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=firstPRD.lag, x=mgPRD, all=T)))

# Vote share at t-1 as the outcome
(rddBal.smd.inc02a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=shInc.lag, x=mgInc, all=T)))
(rddBal.smd.pan02a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=shPAN.lag, x=mgPAN, all=T)))
(rddBal.smd.pri02a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=shPRI.lag, x=mgPRI, all=T)))
(rddBal.smd.prd02a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=shPRD.lag, x=mgPRD, all=T)))

# Population (log) as the outcome
(rddBal.smd.inc03a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=log (population), x=mgInc, all=T)))
(rddBal.smd.pan03a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=log (population), x=mgPAN, all=T)))
(rddBal.smd.pri03a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=log (population), x=mgPRI, all=T)))
(rddBal.smd.prd03a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=log (population), x=mgPRD, all=T)))

# Poverty as the outcome
(rddBal.smd.inc04a <- with (base.d[(base.d$firstInc==1 | base.d$secondInc==1),], rdrobust (y=poverty, x=mgInc, all=T)))
(rddBal.smd.pan04a <- with (base.d[base.d$firstPAN==1 | base.d$secondPAN==1,], rdrobust (y=poverty, x=mgPAN, all=T)))
(rddBal.smd.pri04a <- with (base.d[base.d$firstPRI==1 | base.d$secondPRI==1,], rdrobust (y=poverty, x=mgPRI, all=T)))
(rddBal.smd.prd04a <- with (base.d[base.d$firstPRD==1 | base.d$secondPRD==1,], rdrobust (y=poverty, x=mgPRD, all=T)))




########## BUILDING THE TABLES

### Replicating Table 1
(coefMain01 <- rbind (
  c (with (rddWin2.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# adding the asterisks
coefMain02 <- cbind (matrix (sprintf ("%.2f", round (coefMain01[,c (1:2,4)], 3)), ncol=3)
                     , matrix (sprintf ("%.0f", coefMain01[,5]), ncol=1)
                     , matrix ("", nrow=nrow (coefMain01), ncol=1)
                     , matrix (sprintf ("%.2f", round (coefMain01[,c (6:7,9)], 3)), ncol=3)
                     , matrix (sprintf ("%.0f", coefMain01[,10]), ncol=1))
coefMain02[,2] <- ifelse (coefMain01[,3] <= 0.05, paste (coefMain02[,2], "**", sep=""), ifelse (coefMain01[,3] <= 0.10, paste (coefMain02[,2], "*", sep=""), coefMain02[,2]))
coefMain02[,7] <- ifelse (coefMain01[,8] <= 0.05, paste (coefMain02[,7], "**", sep=""), ifelse (coefMain01[,8] <= 0.10, paste (coefMain02[,7], "*", sep=""), coefMain02[,7]))

rows <- c ("Incumbent", "PAN", "PRI", "PRD")
Header1 <- paste ("\\toprule & \\multicolumn{4}{c}{\\textbf{(a) Mayors}} & & \\multicolumn{4}{c}{\\textbf{(b) Legislators}} \\\\ [0.75ex] \\cmidrule{2-5} \\cmidrule{7-10} \n")
Header2 <- paste ("\\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$command <- c (Header1, Header2)
print (xtable (cbind (rows, coefMain02)
               , align=c("l","c","r","l","c","r","c","r","c","c","r")
               , caption="RD estimates of the effect of incumbency on a party's probability of winning at $t+1$."
               , label="T:allWin")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table 2 --> for this table only, we pasted the values manually (and added the corresponding bandwidth manually as well)

# Panel (a): Close elections
texreg (list (lmClose.inc01a, lmClose.pan01a, lmClose.pri01a, lmClose.prd01a)
        , stars = c (0.05, 0.1)
        , custom.coef.names = c ("constant", rep ("close election", 4))
        , digits=n.digit
        , caption="Effect of \\emph{close elections} on the \\emph{vote share} of the third-placed party. Outcome is the change in \\emph{vote share} of the third-place party between $t$ and $t+1$ (i.e., negative values indicate that the third-placed party \\emph{lost} votes)."
        , caption.above=T
        , label="T:close"
        , fontsize="footnotesize")

# Panel (b): Absolute margin of victory (all observations)
texreg (list (lmClose.inc01b, lmClose.pan01b, lmClose.pri01b, lmClose.prd01b)
        , stars = c (0.05, 0.1)
        , custom.coef.names = c ("constant", rep ("$|$\\emph{margin of victory}$|_t$", 4))
        , digits=n.digit
        , caption="Effect of \\emph{close elections} on the \\emph{vote share} of the third-placed party. Outcome is the change in \\emph{vote share} of the third-place party between $t$ and $t+1$ (i.e., negative values indicate that the third-placed party \\emph{lost} votes)."
        , caption.above=T
        , label="T:close"
        , fontsize="footnotesize")



### Replicating Table 3
(coefDuv <- rbind (
  
  # vote share
  c (with (rddSh2.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # winner (oth)
  , c (with (rddWin2.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # vote share (oth)
  , c (with (rddSh2.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh2.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# re-ordering the rows
coefDuv <- coefDuv[c (seq (1, 12, by=4), seq (2, 12, by=4), seq (3, 12, by=4), seq (4, 12, by=4)),]

# adding the asterisks
coefDuv02 <- cbind (matrix (sprintf ("%.2f", round (coefDuv[,c (1:2,4)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefDuv[,5]), ncol=1)
)
coefDuv02[,2] <- ifelse (coefDuv[,3] <= 0.05, paste (coefDuv02[,2], "**", sep=""), ifelse (coefDuv[,3] <= 0.10, paste (coefDuv02[,2], "*", sep=""), coefDuv02[,2]))

rows <- c (rep ("Incumbent", 3), rep ("PAN", 3), rep ("PRI", 3), rep ("PRD", 3))
rows02 <- c (rep (c ("v. share", "winner (oth)", "v. share (oth)"), 4))
Header1 <- paste ("\\toprule \\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{outcome} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$command <- c (Header1)
print (xtable (cbind (rows, rows02, coefDuv02)
               , align=c("l","c","l","r","l","r","r")
               , caption="Exploring the mechanisms behind negative incumbency effects: Duvergerian coordination."
               , label="T:muniDuv")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table 4
(coefPRI <- rbind (
  
  # PRI vs. PAN
  c (with (rddWin2.pri05a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri05b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # PRI vs. PRD
  , c (with (rddWin2.pri06a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri06b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# adding the asterisks
coefPRI02 <- cbind (matrix (sprintf ("%.2f", round (coefPRI[,c (1:2,4)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefPRI[,5]), ncol=1))
coefPRI02[,2] <- ifelse (coefPRI[,3] <= 0.05, paste (coefPRI02[,2], "**", sep=""), ifelse (coefPRI[,3] <= 0.10, paste (coefPRI02[,2], "*", sep=""), coefPRI02[,2]))

rows <- c (rep ("PRI vs. PAN", 2), rep ("PRI vs. PRD", 2))
rows02 <- c ("winner (PRI)", "winner (PAN)", "winner (PRI)", "winner (PRD)")
Header1 <- paste ("\\toprule \\multicolumn{1}{c}{sample} & \\multicolumn{1}{c}{outcome} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$command <- c (Header1)
print (xtable (cbind (rows, rows02, coefPRI02)
               , align=c("l","c","l","r","l","r","r")
               , caption="Duvergerian coordination in Mexican municipal elections: effect of PRI \\emph{incumbency} on the electoral performance of the PAN and PRD."
               , label="T:muniPRI")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table 5
(coefGov <- rbind (
  
  ## full sample
  c (with (rddWin2.inc03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.inc04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# adding the asterisks
coefGov02 <- cbind (matrix (sprintf ("%.2f", round (coefGov[,c (1:2,4)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefGov[,5]), ncol=1)
)
coefGov02[,2] <- ifelse (coefGov[,3] <= 0.05, paste (coefGov02[,2], "**", sep=""), ifelse (coefGov[,3] <= 0.10, paste (coefGov02[,2], "*", sep=""), coefGov02[,2]))

rows <- c (rep ("Incumbent", 2), rep ("PAN", 2), rep ("PRI", 2), rep ("PRD", 2))
rows02 <- c (rep (c ("Copartisan", "Opposition"), 4))
Header1 <- paste ("\\toprule \\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{governor} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$command <- c (Header1)
print (xtable (cbind (rows, rows02, coefGov02)
               , align=c("l","c","l","r","l","r","r")
               , caption="Exploring the mechanisms behind negative incumbency effects: strategic elite behavior (outcome is \\emph{winner})."
               , label="T:muniGov")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table A1
(descMuni <- with (base, rbind (
  
  # winner (t+1)
  c (mean (firstInc.next, na.rm=T), sd (firstInc.next, na.rm=T), min (firstInc.next, na.rm=T), max (firstInc.next, na.rm=T))
  , c (mean (firstPAN.next, na.rm=T), sd (firstPAN.next, na.rm=T), min (firstPAN.next, na.rm=T), max (firstPAN.next, na.rm=T))
  , c (mean (firstPRI.next, na.rm=T), sd (firstPRI.next, na.rm=T), min (firstPRI.next, na.rm=T), max (firstPRI.next, na.rm=T))
  , c (mean (firstPRD.next, na.rm=T), sd (firstPRD.next, na.rm=T), min (firstPRD.next, na.rm=T), max (firstPRD.next, na.rm=T))
  
  # vote share (t+1)
  , c (mean (shInc.muni.next, na.rm=T), sd (shInc.muni.next, na.rm=T), min (shInc.muni.next, na.rm=T), max (shInc.muni.next, na.rm=T))
  , c (mean (shPAN.muni.next, na.rm=T), sd (shPAN.muni.next, na.rm=T), min (shPAN.muni.next, na.rm=T), max (shPAN.muni.next, na.rm=T))
  , c (mean (shPRI.muni.next, na.rm=T), sd (shPRI.muni.next, na.rm=T), min (shPRI.muni.next, na.rm=T), max (shPRI.muni.next, na.rm=T))
  , c (mean (shPRD.muni.next, na.rm=T), sd (shPRD.muni.next, na.rm=T), min (shPRD.muni.next, na.rm=T), max (shPRD.muni.next, na.rm=T))
  
  # winner (t)
  , c (mean (winInc.muni, na.rm=T), sd (winInc.muni, na.rm=T), min (winInc.muni, na.rm=T), max (winInc.muni, na.rm=T))
  , c (mean (firstPAN, na.rm=T), sd (firstPAN, na.rm=T), min (firstPAN, na.rm=T), max (firstPAN, na.rm=T))
  , c (mean (firstPRI, na.rm=T), sd (firstPRI, na.rm=T), min (firstPRI, na.rm=T), max (firstPRI, na.rm=T))
  , c (mean (firstPRD, na.rm=T), sd (firstPRD, na.rm=T), min (firstPRD, na.rm=T), max (firstPRD, na.rm=T))
  
  # margin of victory (t)
  , c (mean (mgInc.muni.rdd, na.rm=T), sd (mgInc.muni.rdd, na.rm=T), min (mgInc.muni.rdd, na.rm=T), max (mgInc.muni.rdd, na.rm=T))
  , c (mean (mgPAN.muni.rdd, na.rm=T), sd (mgPAN.muni.rdd, na.rm=T), min (mgPAN.muni.rdd, na.rm=T), max (mgPAN.muni.rdd, na.rm=T))
  , c (mean (mgPRI.muni.rdd, na.rm=T), sd (mgPRI.muni.rdd, na.rm=T), min (mgPRI.muni.rdd, na.rm=T), max (mgPRI.muni.rdd, na.rm=T))
  , c (mean (mgPRD.muni.rdd, na.rm=T), sd (mgPRD.muni.rdd, na.rm=T), min (mgPRD.muni.rdd, na.rm=T), max (mgPRD.muni.rdd, na.rm=T))
  
  # copartisan governor (t+1)
  , c (mean (gobInc.muni, na.rm=T), sd (gobInc.muni, na.rm=T), min (gobInc.muni, na.rm=T), max (gobInc.muni, na.rm=T))
  , c (mean (gobPAN.muni, na.rm=T), sd (gobPAN.muni, na.rm=T), min (gobPAN.muni, na.rm=T), max (gobPAN.muni, na.rm=T))
  , c (mean (gobPRI.muni, na.rm=T), sd (gobPRI.muni, na.rm=T), min (gobPRI.muni, na.rm=T), max (gobPRI.muni, na.rm=T))
  , c (mean (gobPRD.muni, na.rm=T), sd (gobPRD.muni, na.rm=T), min (gobPRD.muni, na.rm=T), max (gobPRD.muni, na.rm=T))
)))

(descSMD <- with (base.d, rbind (
  
  # winner (t+1)
  c (mean (firstInc.next, na.rm=T), sd (firstInc.next, na.rm=T), min (firstInc.next, na.rm=T), max (firstInc.next, na.rm=T))
  , c (mean (firstPAN.next, na.rm=T), sd (firstPAN.next, na.rm=T), min (firstPAN.next, na.rm=T), max (firstPAN.next, na.rm=T))
  , c (mean (firstPRI.next, na.rm=T), sd (firstPRI.next, na.rm=T), min (firstPRI.next, na.rm=T), max (firstPRI.next, na.rm=T))
  , c (mean (firstPRD.next, na.rm=T), sd (firstPRD.next, na.rm=T), min (firstPRD.next, na.rm=T), max (firstPRD.next, na.rm=T))
  
  # vote share (t+1)
  , c (mean (shInc.next, na.rm=T), sd (shInc.next, na.rm=T), min (shInc.next, na.rm=T), max (shInc.next, na.rm=T))
  , c (mean (shPAN.next, na.rm=T), sd (shPAN.next, na.rm=T), min (shPAN.next, na.rm=T), max (shPAN.next, na.rm=T))
  , c (mean (shPRI.next, na.rm=T), sd (shPRI.next, na.rm=T), min (shPRI.next, na.rm=T), max (shPRI.next, na.rm=T))
  , c (mean (shPRD.next, na.rm=T), sd (shPRD.next, na.rm=T), min (shPRD.next, na.rm=T), max (shPRD.next, na.rm=T))
  
  # winner (t)
  , c (mean (firstInc, na.rm=T), sd (firstInc, na.rm=T), min (firstInc, na.rm=T), max (firstInc, na.rm=T))
  , c (mean (firstPAN, na.rm=T), sd (firstPAN, na.rm=T), min (firstPAN, na.rm=T), max (firstPAN, na.rm=T))
  , c (mean (firstPRI, na.rm=T), sd (firstPRI, na.rm=T), min (firstPRI, na.rm=T), max (firstPRI, na.rm=T))
  , c (mean (firstPRD, na.rm=T), sd (firstPRD, na.rm=T), min (firstPRD, na.rm=T), max (firstPRD, na.rm=T))
  
  # margin of victory (t)
  , c (mean (mgInc, na.rm=T), sd (mgInc, na.rm=T), min (mgInc, na.rm=T), max (mgInc, na.rm=T))
  , c (mean (mgPAN, na.rm=T), sd (mgPAN, na.rm=T), min (mgPAN, na.rm=T), max (mgPAN, na.rm=T))
  , c (mean (mgPRI, na.rm=T), sd (mgPRI, na.rm=T), min (mgPRI, na.rm=T), max (mgPRI, na.rm=T))
  , c (mean (mgPRD, na.rm=T), sd (mgPRD, na.rm=T), min (mgPRD, na.rm=T), max (mgPRD, na.rm=T))
  
  # copartisan governor (t+1)
  , c (mean (gobInc, na.rm=T), sd (gobInc, na.rm=T), min (gobInc, na.rm=T), max (gobInc, na.rm=T))
  , c (mean (gobPAN, na.rm=T), sd (gobPAN, na.rm=T), min (gobPAN, na.rm=T), max (gobPAN, na.rm=T))
  , c (mean (gobPRI, na.rm=T), sd (gobPRI, na.rm=T), min (gobPRI, na.rm=T), max (gobPRI, na.rm=T))
  , c (mean (gobPRD, na.rm=T), sd (gobPRD, na.rm=T), min (gobPRD, na.rm=T), max (gobPRD, na.rm=T))
)))

tab.desc <- cbind (matrix (sprintf ("%.2f", round (descMuni, 3)), ncol=4)
                   , matrix ("", nrow=nrow (descMuni), ncol=1)
                   , matrix (sprintf ("%.2f", round (descSMD, 3)), ncol=4))
rows01 <- c ("winner (t+1) (Incumbent)", "winner (t+1) (PAN)", "winner (t+1) (PRI)", "winner (t+1) (PRD)"
             , "vote share (t+1) (Incumbent)", "vote share (t+1) (PAN)", "vote share (t+1) (PRI)", "vote share (t+1) (PRD)"
             , "winner (t) (Incumbent)", "winner (t) (PAN)", "winner (t) (PRI)", "winner (t) (PRD)"
             , "margin of victory (t) (Inc.)", "margin of victory (t) (PAN)", "margin of victory (t) (PRI)", "margin of victory (t) (PRD)"
             , "copartisan governor (t+1) (Inc.)", "copartisan governor (t+1) (PAN)", "copartisan governor (t+1) (PRI)", "copartisan governor (t+1) (PRD)")
Header1 <- paste ("\\toprule & \\multicolumn{4}{c}{\\textbf{(a) Municipalities ($N=", sprintf ("%.0f", round (nrow (base))), "$)}} & & \\multicolumn{4}{c}{\\textbf{(b) Congressional SMDs ($N=", sprintf ("%.0f", round (nrow (base.d))), "$)}} \\\\ [0.75ex] \\cmidrule{2-5} \\cmidrule{7-10} \n")
Header2 <- paste ("\\multicolumn{1}{l}{Dependent variables} & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{SD} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} & & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{SD} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} \\\\ \\otoprule \n")
Header3 <- paste ("[1.5ex] \\multicolumn{10}{l}{Explanatory variables} \\\\ \\otoprule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 8
addtorow$command <- c (Header1, Header2, Header3)
print (xtable (cbind (rows02, tab.desc)
               , align=c("l","l","r","r","r","r","c","r","r","r","r")
               , digits=n.digit
               , caption="Descriptive statistics."
               , label="T:descriptive")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table A2
(coefWinApp <- rbind (
  
  # full sample
  c (with (rddWin2.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # non-concurrent states only
  , c (with (rddWin2.inc01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd01b, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # concurrent states only
  , c (with (rddWin2.inc01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pan01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.pri01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin2.prd01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd01c, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# adding the asterisks
coefWinApp02 <- cbind (matrix (sprintf ("%.2f", round (coefWinApp[,c (1:2,4)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefWinApp[,5]), ncol=1)
                       , matrix ("", nrow=nrow (coefWinApp), ncol=1)
                       , matrix (sprintf ("%.2f", round (coefWinApp[,c (6:7,9)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefWinApp[,10]), ncol=1))
coefWinApp02[,2] <- ifelse (coefWinApp[,3] <= 0.05, paste (coefWinApp02[,2], "**", sep=""), ifelse (coefWinApp[,3] <= 0.10, paste (coefWinApp02[,2], "*", sep=""), coefWinApp02[,2]))
coefWinApp02[,7] <- ifelse (coefWinApp[,8] <= 0.05, paste (coefWinApp02[,7], "**", sep=""), ifelse (coefWinApp[,8] <= 0.10, paste (coefWinApp02[,7], "*", sep=""), coefWinApp02[,7]))

rows <- rep (c ("Incumbent", "PAN", "PRI", "PRD"), 3)
Header1 <- paste ("\\toprule & \\multicolumn{4}{c}{\\textbf{(a) Mayors}} & & \\multicolumn{4}{c}{\\textbf{(b) Legislators}} \\\\ [0.75ex] \\cmidrule{2-5} \\cmidrule{7-10} \n")
Header2 <- paste ("\\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")
Header3 <- paste ("\\multicolumn{10}{l}{\\underline{(a) Full sample}} \\\\ [1ex] \n")
Header4 <- paste ("\\multicolumn{10}{l}{\\underline{(b) Non-concurrent states only}} \\\\ [1ex] \n")
Header5 <- paste ("\\multicolumn{10}{l}{\\underline{(c) Concurrent states only}} \\\\ [1ex] \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 4
addtorow$pos[[5]] <- 8
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5)
print (xtable (cbind (rows, coefWinApp02)
               , align=c("l","c","r","l","c","r","c","r","c","c","r")
               , caption="RD estimates of the effect of incumbency on a party's probability of winning at $t+1$, distinguishing between concurrent or non-concurrent elections."
               , label="T:allWinAppendix")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table A3
(coefDuvFed01 <- rbind (
  
  # winner
  c (with (rddWin.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # vote share
  , c (with (rddSh.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.inc01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # winner (other)
  , c (with (rddWin.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  
  # vote share (other)
  , c (with (rddSh.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddSh.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddSh.smd.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# re-ordering the rows
coefDuvFed01 <- coefDuvFed01[c (seq (1, 16, by=4), seq (2, 16, by=4), seq (3, 16, by=4), seq (4, 16, by=4)),]

# adding the asterisks
coefDuvFed02 <- cbind (matrix (sprintf ("%.2f", round (coefDuvFed01[,c (1:2,4)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefDuvFed01[,5]), ncol=1)
                       , matrix ("", nrow=nrow (coefDuvFed01), ncol=1)
                       , matrix (sprintf ("%.2f", round (coefDuvFed01[,c (6:7,9)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefDuvFed01[,10]), ncol=1))
coefDuvFed02[,2] <- ifelse (coefDuvFed01[,3] <= 0.05, paste (coefDuvFed02[,2], "**", sep=""), ifelse (coefDuvFed01[,3] <= 0.10, paste (coefDuvFed02[,2], "*", sep=""), coefDuvFed02[,2]))
coefDuvFed02[,7] <- ifelse (coefDuvFed01[,8] <= 0.05, paste (coefDuvFed02[,7], "**", sep=""), ifelse (coefDuvFed01[,8] <= 0.10, paste (coefDuvFed02[,7], "*", sep=""), coefDuvFed02[,7]))

rows <- c (rep ("Incumbent", 4), rep ("PAN", 4), rep ("PRI", 4), rep ("PRD", 4))
rows02 <- c (rep (c ("winner", "v. share", "winner (oth)", "v. share (oth)"), 4))
Header1 <- paste ("\\toprule & & \\multicolumn{4}{c}{\\textbf{(a) Municipalities}} & & \\multicolumn{4}{c}{\\textbf{(b) SMDs}} \\\\ [0.75ex] \\cmidrule{3-6} \\cmidrule{8-11} \n")
Header2 <- paste ("\\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{outcome} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$command <- c (Header1, Header2)
print (xtable (cbind (rows, coefDuvFed02)
               , align=c("l","c","r","l","c","r","c","r","c","c","r")
               , caption="Exploring the mechanisms behind negative incumbency effects: Balancing in \\emph{federal} elections."
               , label="T:smdDuvFed")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table A4
(coefGovFed01 <- rbind (
  
  # copartisna governor
  c (with (rddWin.inc03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.inc04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.inc04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pan03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pan04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pan04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pri03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.pri04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.pri04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.prd03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddWin.prd04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddWin.smd.prd04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

# adding the asterisks
coefGovFed02 <- cbind (matrix (sprintf ("%.2f", round (coefGovFed01[,c (1:2,4)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefGovFed01[,5]), ncol=1)
                       , matrix ("", nrow=nrow (coefGovFed01), ncol=1)
                       , matrix (sprintf ("%.2f", round (coefGovFed01[,c (6:7,9)], 3)), ncol=3)
                       , matrix (sprintf ("%.0f", coefGovFed01[,10]), ncol=1))
coefGovFed02[,2] <- ifelse (coefGovFed01[,3] <= 0.05, paste (coefGovFed02[,2], "**", sep=""), ifelse (coefGovFed01[,3] <= 0.10, paste (coefGovFed02[,2], "*", sep=""), coefGovFed02[,2]))
coefGovFed02[,7] <- ifelse (coefGovFed01[,8] <= 0.05, paste (coefGovFed02[,7], "**", sep=""), ifelse (coefGovFed01[,8] <= 0.10, paste (coefGovFed02[,7], "*", sep=""), coefGovFed02[,7]))

rows <- c (rep ("Incumbent", 2), rep ("PAN", 2), rep ("PRI", 2), rep ("PRD", 2))
rows02 <- c (rep (c ("Copartisan", "Opposition"), 4))
Header1 <- paste ("\\toprule \\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{governor} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

## elections to the same office
addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$command <- c (Header1)
print (xtable (cbind (rows, rows02, coefGovFed02)
               , align=c("l","c","c","r","l","c","r","c","r","c","c","r")
               , caption="Exploring the mechanisms behind negative incumbency effects: Strategic elite behavior (outcome is \\emph{winner}) (federal elections)."
               , label="T:smdGov")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)



### Replicating Table A5
(coefBal <- rbind (
  c (with (rddBal.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.inc02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.inc03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.inc03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.inc04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.inc04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pan01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pan02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pan03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pan03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pan04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pan04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pri01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pri02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pri03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pri03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.pri04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.pri04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.prd01a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.prd02a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.prd03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.prd03a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
  , c (with (rddBal.prd04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)), with (rddBal.smd.prd04a, c (coef[2], se[2], pv[2], bws[1], N_l+N_r)))
))

## adding the asterisks
coefBal02 <- cbind (matrix (sprintf ("%.2f", round (coefBal[,c (1:2,4)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefBal[,5]), ncol=1)
                    , matrix ("", nrow=nrow (coefBal), ncol=1)
                    , matrix (sprintf ("%.2f", round (coefBal[,c (6:7,9)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefBal[,10]), ncol=1))
coefBal02[,2] <- ifelse (coefBal[,3] <= 0.05, paste (coefBal02[,2], "**", sep=""), ifelse (coefBal[,3] <= 0.10, paste (coefBal02[,2], "*", sep=""), coefBal02[,2]))
coefBal02[,7] <- ifelse (coefBal[,8] <= 0.05, paste (coefBal02[,7], "**", sep=""), ifelse (coefBal[,8] <= 0.10, paste (coefBal02[,7], "*", sep=""), coefBal02[,7]))

rows <- c (rep ("Incumbent", 3), rep ("PAN", 4), rep ("PRI", 4), rep ("PRD", 4))
rows02 <- c ("vote share (t-1)", "population (log)", "poverty", rep (c ("winner (t-1)", "vote share (t-1)", "population (log)", "poverty"), 3))
Header1 <- paste ("\\toprule & & \\multicolumn{4}{c}{\\textbf{(a) Municipalities}} & & \\multicolumn{4}{c}{\\textbf{(b) SMDs}} \\\\ [0.75ex] \\cmidrule{3-6} \\cmidrule{8-11} \n")
Header2 <- paste ("\\multicolumn{1}{c}{party} & \\multicolumn{1}{c}{outcome} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N$} \\\\ \\otoprule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$command <- c (Header1, Header2)
print (xtable (cbind (rows, rows02, coefBal02)
               , align=c("l","c","l","r","l","r","r","c","r","l","r","r")
               , digits=n.digit
               , caption="Balance checks."
               , label="T:balance")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)





########## BUILDING THE FIGURES

### Replicating Figure 1

(aspect.ratio <- 4/1)
plotWidth <- 1200

## Top panel: Municipal elections
png ("fig_MuniWin_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base, rdplot (y=firstInc.next, x=mgInc.muni.rdd, p=1
                    , subset=(base$firstInc.muni==1 | base$secondInc.muni==1) & abs(mgInc.muni.rdd)<=0.15
                    , scale=1
                    , title="Incumbent party at t"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="Pr[win municipal election (t+1)]"))
with (base, rdplot (y=firstPAN.next, x=mgPAN.muni.rdd, p=1
                    , subset=(base$firstPAN==1 | base$secondPAN==1) & abs(mgPAN.muni.rdd)<=0.15
                    , scale=1
                    , title="PAN"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label=""))
with (base, rdplot (y=firstPRI.next, x=mgPRI.muni.rdd, p=1
                    , subset=(base$firstPRI==1 | base$secondPRI==1) & abs(mgPRI.muni.rdd)<=0.15
                    , scale=1
                    , title="PRI"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label=""))
with (base, rdplot (y=firstPRD.next, x=mgPRD.muni.rdd, p=1
                    , subset=(base$firstPRD==1 | base$secondPRD==1) & abs(mgPRD.muni.rdd)<=0.15
                    , scale=1
                    , title="PRD"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label=""))
dev.off ()


## Bottom panel: SMD elections
png ("fig_SMDWin_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base.d, rdplot (y=firstInc.next, x=mgInc, p=1
                      , subset=(base.d$firstInc==1 | base.d$secondInc==1) & abs(mgInc)<=0.15
                      , scale=1
                      , title="Incumbent party at t"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="Pr[win SMD election (t+1)]"))
with (base.d, rdplot (y=firstPAN.next, x=mgPAN, p=1
                      , subset=(base.d$firstPAN==1 | base.d$secondPAN==1) & abs(mgPAN)<=0.15
                      , scale=1
                      , title="PAN"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label=""))
with (base.d, rdplot (y=firstPRI.next, x=mgPRI, p=1
                      , subset=(base.d$firstPRI==1 | base.d$secondPRI==1) & abs(mgPRI)<=0.15
                      , scale=1
                      , title="PRI"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label=""))
with (base.d, rdplot (y=firstPRD.next, x=mgPRD, p=1
                      , subset=(base.d$firstPRD==1 | base.d$secondPRD==1) & abs(mgPRD)<=0.15
                      , scale=1
                      , title="PRD"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label=""))
dev.off ()



### Replicating Figure A1

(aspect.ratio <- 4/1)
plotWidth <- 1200

# (a) Incumbent party
png ("fig_muniBalInc_fullSample.png", w=plotWidth*3/4, h=plotWidth/aspect.ratio)
par (mfrow=c (1,3), mar=c (4.5,5,3,0.5))
with (base, rdplot (y=shInc.lag, x=mgInc.muni.rdd, p=1
                    , subset=(base$firstInc.muni==1 | base$secondInc.muni==1) & abs(mgInc.muni.rdd)<=0.15
                    , y.lim=c (0.2, 0.8)
                    , scale=1
                    , title="vote share (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="vote share (t-1)"))
with (base, rdplot (y=log (population), x=mgInc.muni.rdd, p=1
                    , subset=(base$firstInc.muni==1 | base$secondInc.muni==1) & abs(mgInc.muni.rdd)<=0.15
                    , y.lim=c (8, 12)
                    , scale=1
                    , title="population"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="population (log)"))
with (base, rdplot (y=poverty, x=mgInc.muni.rdd, p=1
                    , subset=(base$firstInc.muni==1 | base$secondInc.muni==1) & abs(mgInc.muni.rdd)<=0.15
                    , y.lim=c (-1.2, 0.8)
                    , scale=1
                    , title="poverty"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="poverty"))
dev.off ()

# (b) PAN
png ("fig_muniBalPAN_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base, rdplot (y=firstPAN.lag, x=mgPAN.muni.rdd, p=1
                    , subset=(base$firstPAN==1 | base$secondPAN==1) & abs(mgPAN.muni.rdd)<=0.15
                    , y.lim=c (0, 0.6)
                    , scale=1
                    , title="winner (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="Pr[won municipal election (t-1)]"))
with (base, rdplot (y=shPAN.lag, x=mgPAN.muni.rdd, p=1
                    , subset=(base$firstPAN==1 | base$secondPAN==1) & abs(mgPAN.muni.rdd)<=0.15
                    , y.lim=c (0, 0.6)
                    , scale=1
                    , title="vote share (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="vote share (t-1)"))
with (base, rdplot (y=log (population), x=mgPAN.muni.rdd, p=1
                    , subset=(base$firstPAN==1 | base$secondPAN==1) & abs(mgPAN.muni.rdd)<=0.15
                    , y.lim=c (8, 12)
                    , scale=1
                    , title="population"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="population (log)"))
with (base, rdplot (y=poverty, x=mgPAN.muni.rdd, p=1
                    , subset=(base$firstPAN==1 | base$secondPAN==1) & abs(mgPAN.muni.rdd)<=0.15
                    , y.lim=c (-1.2, 0.8)
                    , scale=1
                    , title="poverty"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="poverty"))
dev.off ()

# (c) PRI
png ("fig_muniBalPRI_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base, rdplot (y=firstPRI.lag, x=mgPRI.muni.rdd, p=1
                    , subset=(base$firstPRI==1 | base$secondPRI==1) & abs(mgPRI.muni.rdd)<=0.15
                    , y.lim=c (0.2, 1)
                    , scale=1
                    , title="winner (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="Pr[won municipal election (t-1)]"))
with (base, rdplot (y=shPRI.lag, x=mgPRI.muni.rdd, p=1
                    , subset=(base$firstPRI==1 | base$secondPRI==1) & abs(mgPRI.muni.rdd)<=0.15
                    , y.lim=c (0.2, 1)
                    , scale=1
                    , title="vote share (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="vote share (t-1)"))
with (base, rdplot (y=log (population), x=mgPRI.muni.rdd, p=1
                    , subset=(base$firstPRI==1 | base$secondPRI==1) & abs(mgPRI.muni.rdd)<=0.15
                    , y.lim=c (8, 12)
                    , scale=1
                    , title="population"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="population (log)"))
with (base, rdplot (y=poverty, x=mgPRI.muni.rdd, p=1
                    , subset=(base$firstPRI==1 | base$secondPRI==1) & abs(mgPRI.muni.rdd)<=0.15
                    , y.lim=c (-1.2, 0.8)
                    , scale=1
                    , title="poverty"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="poverty"))
dev.off ()

# (d) PRD
png ("fig_muniBalPRD_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base, rdplot (y=firstPRD.lag, x=mgPRD.muni.rdd, p=1
                    , subset=(base$firstPRD==1 | base$secondPRD==1) & abs(mgPRD.muni.rdd)<=0.15
                    , y.lim=c (0, 0.6)
                    , scale=1
                    , title="winner (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="Pr[won municipal election (t-1)]"))
with (base, rdplot (y=shPRD.lag, x=mgPRD.muni.rdd, p=1
                    , subset=(base$firstPRD==1 | base$secondPRD==1) & abs(mgPRD.muni.rdd)<=0.15
                    , y.lim=c (0, 0.6)
                    , scale=1
                    , title="vote share (t-1)"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="vote share (t-1)"))
with (base, rdplot (y=log (population), x=mgPRD.muni.rdd, p=1
                    , subset=(base$firstPRD==1 | base$secondPRD==1) & abs(mgPRD.muni.rdd)<=0.15
                    , y.lim=c (8, 12)
                    , scale=1
                    , title="population"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="population (log)"))
with (base, rdplot (y=poverty, x=mgPRD.muni.rdd, p=1
                    , subset=(base$firstPRD==1 | base$secondPRD==1) & abs(mgPRD.muni.rdd)<=0.15
                    , y.lim=c (-1.2, 0.8)
                    , scale=1
                    , title="poverty"
                    , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                    , col.dots="black", col.lines="black"
                    , x.label="margin of victory in municipal election (t)"
                    , y.label="poverty"))
dev.off ()



### Replicating Figure A2

(aspect.ratio <- 4/1)
plotWidth <- 1200

# (a) Incumbent party
png ("fig_SMDBalInc_fullSample.png", w=plotWidth*3/4, h=plotWidth/aspect.ratio)
par (mfrow=c (1,3), mar=c (4.5,5,3,0.5))
with (base.d, rdplot (y=shInc.lag, x=mgInc, p=1
                      , subset=(base.d$firstInc==1 | base.d$secondInc==1) & abs(mgInc)<=0.15
                      , y.lim=c (0.2, 0.8)
                      , scale=1
                      , title="vote share (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="vote share (t-1)"))
with (base.d, rdplot (y=log (population), x=mgInc, p=1
                      , subset=(base.d$firstInc==1 | base.d$secondInc==1) & abs(mgInc)<=0.15
                      , y.lim=c (12.5, 13)
                      , scale=1
                      , title="population"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="population (log)"))
with (base.d, rdplot (y=poverty, x=mgInc, p=1
                      , subset=(base.d$firstInc==1 | base.d$secondInc==1) & abs(mgInc)<=0.15
                      , y.lim=c (-1.2, 1.4)
                      , scale=1
                      , title="poverty"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="poverty"))
dev.off ()

# (b) PAN
png ("fig_SMDBalPAN_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base.d, rdplot (y=firstPAN.lag, x=mgPAN, p=1
                      , subset=(base.d$firstPAN==1 | base.d$secondPAN==1) & abs(mgPAN)<=0.15
                      , y.lim=c (0, 0.8)
                      , scale=1
                      , title="winner (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="Pr[won municipal election (t-1)]"))
with (base.d, rdplot (y=shPAN.lag, x=mgPAN, p=1
                      , subset=(base.d$firstPAN==1 | base.d$secondPAN==1) & abs(mgPAN)<=0.15
                      , y.lim=c (0, 0.8)
                      , scale=1
                      , title="vote share (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="vote share (t-1)"))
with (base.d, rdplot (y=log (population), x=mgPAN, p=1
                      , subset=(base.d$firstPAN==1 | base.d$secondPAN==1) & abs(mgPAN)<=0.15
                      , y.lim=c (12.5, 13)
                      , scale=1
                      , title="population"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="population (log)"))
with (base.d, rdplot (y=poverty, x=mgPAN, p=1
                      , subset=(base.d$firstPAN==1 | base.d$secondPAN==1) & abs(mgPAN)<=0.15
                      , y.lim=c (-1.2, 1.4)
                      , scale=1
                      , title="poverty"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="poverty"))
dev.off ()

# (c) PRI
png ("fig_SMDBalPRI_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base.d, rdplot (y=firstPRI.lag, x=mgPRI, p=1
                      , subset=(base.d$firstPRI==1 | base.d$secondPRI==1) & abs(mgPRI)<=0.15
                      , y.lim=c (0.2, 1)
                      , scale=1
                      , title="winner (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="Pr[won municipal election (t-1)]"))
with (base.d, rdplot (y=shPRI.lag, x=mgPRI, p=1
                      , subset=(base.d$firstPRI==1 | base.d$secondPRI==1) & abs(mgPRI)<=0.15
                      , y.lim=c (0.2, 1)
                      , scale=1
                      , title="vote share (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="vote share (t-1)"))
with (base.d, rdplot (y=log (population), x=mgPRI, p=1
                      , subset=(base.d$firstPRI==1 | base.d$secondPRI==1) & abs(mgPRI)<=0.15
                      , y.lim=c (12.5, 13)
                      , scale=1
                      , title="population"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="population (log)"))
with (base.d, rdplot (y=poverty, x=mgPRI, p=1
                      , subset=(base.d$firstPRI==1 | base.d$secondPRI==1) & abs(mgPRI)<=0.15
                      , y.lim=c (-1.2, 1.4)
                      , scale=1
                      , title="poverty"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="poverty"))
dev.off ()

# (d) PRD
png ("fig_SMDBalPRD_fullSample.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mfrow=c (1,4), mar=c (4.5,5,3,0.5))
with (base.d, rdplot (y=firstPRD.lag, x=mgPRD, p=1
                      , subset=(base.d$firstPRD==1 | base.d$secondPRD==1) & abs(mgPRD)<=0.15
                      , y.lim=c (0, 0.8)
                      , scale=1
                      , title="winner (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="Pr[won municipal election (t-1)]"))
with (base.d, rdplot (y=shPRD.lag, x=mgPRD, p=1
                      , subset=(base.d$firstPRD==1 | base.d$secondPRD==1) & abs(mgPRD)<=0.15
                      , y.lim=c (0, 0.8)
                      , scale=1
                      , title="vote share (t-1)"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="vote share (t-1)"))
with (base.d, rdplot (y=log (population), x=mgPRD, p=1
                      , subset=(base.d$firstPRD==1 | base.d$secondPRD==1) & abs(mgPRD)<=0.15
                      , y.lim=c (12.5, 13)
                      , scale=1
                      , title="population"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="population (log)"))
with (base.d, rdplot (y=poverty, x=mgPRD, p=1
                      , subset=(base.d$firstPRD==1 | base.d$secondPRD==1) & abs(mgPRD)<=0.15
                      , y.lim=c (-1.2, 1.4)
                      , scale=1
                      , title="poverty"
                      , cex.main=1.75, cex.lab=1.5, cex.axis=1.25
                      , col.dots="black", col.lines="black"
                      , x.label="margin of victory in SMD election (t)"
                      , y.label="poverty"))
dev.off ()
